using AutoMapper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ShopPowerTemplate.Entities;
using ShopPowerTemplate.IoC;
using ShopPowerTemplate.Presentation.Mvc.Controllers;
using ShopPowerTemplate.Presentation.Mvc.Extensions.BootStrapper;
using ShopPowerTemplate.Presentation.Mvc.ViewModels;
using ShopPowerTemplate.Services.Tests;
using System.Web.Mvc;

namespace ShopPowerTemplate.Presentation.Mvc.Test
{
    [TestClass]
    public class OrderStateControllerTestBase
    {
        [TestInitialize]
        public virtual void TestInitialize()
        {
            IBootStrapper bootStrapper = new DefaultBootStrapper(IoCFactory.Instance.CurrentContainer);
            bootStrapper.Boot();
        }

        [TestMethod]
        public virtual void OrderStateController_IndexTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OrderStateController>());

            var result = controller.Index() as ViewResult;
            
            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is IndexOrderStateViewModel);
        }

        [TestMethod]
        public virtual void OrderStateController_LoadTest()
        {
            OrderStateServiceUtil.CreateOrderState();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OrderStateController>());

            var result = controller.Load("", "", 0, 10, "") as JsonResult;

            var rows = ModelHelper.GetList(result.Data);
            Assert.IsTrue(rows.Count > 0);
        }

        [TestMethod]
        public virtual void OrderStateController_DeleteTest()
        {
            OrderState orderState = OrderStateServiceUtil.CreateOrderState();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OrderStateController>());

            var result = controller.Delete(orderState.Id) as ContentResult;

            Assert.IsNotNull(result);
            Assert.IsTrue(string.IsNullOrEmpty(result.Content));
        }

        [TestMethod]
        public virtual void OrderStateController_ReportTest()
        {
            OrderState orderState = OrderStateServiceUtil.CreateOrderState();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OrderStateController>());

            var result = controller.Report() as FileContentResult;

            Assert.IsNotNull(result);
            Assert.IsTrue(result.ContentType == "text/csv");
            Assert.IsNotNull(result.FileContents.Length > 0);
        }

        [TestMethod]
        public virtual void OrderStateController_GetCreateTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OrderStateController>());

            var result = controller.Create() as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is CreateOrderStateViewModel);
        }

        [TestMethod]
        public virtual void OrderStateController_PostCreateTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OrderStateController>());
            OrderState orderState = OrderStateServiceUtil.GetOrderState();
            CreateOrderStateViewModel viewModel = Mapper.Map<OrderState, CreateOrderStateViewModel>(orderState);

            var result = controller.Create(viewModel) as RedirectToRouteResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.RouteValues["action"], "Edit"); //Create redirect to edit action on success
            Assert.IsTrue((int)result.RouteValues["id"] > 0); //Check new item id
        }

        [TestMethod]
        public virtual void OrderStateController_GetEditTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OrderStateController>());
            OrderState orderState = OrderStateServiceUtil.CreateOrderState();

            var result = controller.Edit(orderState.Id) as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is EditOrderStateViewModel);
            Assert.AreEqual((result.Model as EditOrderStateViewModel).Name, orderState.Name);
        }

        [TestMethod]
        public virtual void OrderStateController_PostEditTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OrderStateController>());
            OrderState orderState = OrderStateServiceUtil.CreateOrderState();
            orderState.Name = "Test";

            EditOrderStateViewModel viewModel = Mapper.Map<OrderState, EditOrderStateViewModel>(orderState);
            controller.Edit(viewModel);

            var result = controller.Edit(orderState.Id) as ViewResult;
            Assert.IsNotNull(result);
            Assert.AreEqual((result.Model as EditOrderStateViewModel).Name, "Test");
        }

        [TestMethod]
        public virtual void OrderStateController_DetailTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OrderStateController>());
            OrderState orderState = OrderStateServiceUtil.CreateOrderState();

            var result = controller.Details(orderState.Id) as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is DetailsOrderStateViewModel);
            Assert.AreEqual((result.Model as DetailsOrderStateViewModel).Name, orderState.Name);
        }

		/* Details region test */
        [TestMethod]
        public virtual void OrderStateController_LoadOrdersTest()
        {
            Order orders = OrderServiceUtil.CreateOrderFull();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OrderStateController>());

            var result = controller.LoadOrders("", "", 0, 10, "", 1, orders.OrderState.Id) as JsonResult;

            var rows = ModelHelper.GetList(result.Data);
            Assert.IsTrue(rows.Count > 0);
        }
        
        [TestMethod]
        public virtual void OrderStateController_AddOrdersTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OrderStateController>());
            Order orders = OrderServiceUtil.GetOrderFull();
            PopUpOrderViewModel viewModel = Mapper.Map<Order, PopUpOrderViewModel>(orders);

            var result = controller.AddOrders(viewModel) as ContentResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.Content, "OK");
        }

        [TestMethod]
        public virtual void OrderStateController_GetEditOrdersTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OrderStateController>());
            Order orders = OrderServiceUtil.CreateOrderFull();

            var result = controller.EditOrders(orders.Id, orders.OrderState.Id) as PartialViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.AreEqual(result.ViewName, "~/Views/Order/PopUp.cshtml");
            Assert.IsTrue(result.Model is PopUpOrderViewModel);
			Assert.AreEqual((result.Model as PopUpOrderViewModel).Reference, orders.Reference);
            
        }

        [TestMethod]
        public virtual void OrderStateController_PostEditOrdersTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OrderStateController>());
            Order orders = OrderServiceUtil.CreateOrderFull();
            orders.Reference = "Test";

            PopUpOrderViewModel viewModel = Mapper.Map<Order, PopUpOrderViewModel>(orders);
            controller.EditOrders(viewModel);

            var result = controller.EditOrders(orders.Id) as PartialViewResult;
            Assert.IsNotNull(result);
			Assert.AreEqual((result.Model as PopUpOrderViewModel).Reference, "Test");
        }

        [TestMethod]
        public virtual void OrderStateController_DeleteOrdersTest()
        {
            Order orders = OrderServiceUtil.CreateOrderFull();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<OrderStateController>());

            controller.DeleteOrders(orders.Id, orders.OrderState.Id);

            Assert.AreEqual(controller.Response.StatusCode, 200);
        }

    }
}

